Imports Microsoft.VisualBasic

Imports System.Data.Common
Imports System.Data
Imports System.Data.SqlClient
Public Class Ayuda
    Public Sub FillMenu(ByVal MenuControl As Menu, _
                       ByVal DataMenu As SqlDataReader, _
                       ByRef MenuItemPreviousValue As String, _
                       ByRef MenuItemUp As MenuItem)

        If Not DataMenu.HasRows Then Exit Sub

        Dim MenuItemPrevious As MenuItem = Nothing
        Dim MenuItemCurrentValue As String = String.Empty
        Dim MenuItemUpValue As String = String.Empty
        Dim MenuItemCurrent As MenuItem = Nothing
        Dim MenuItemTemp As MenuItem = Nothing

        Dim Lx, lProx, lSup As Integer

        Do
            MenuItemCurrentValue = DataMenu.GetString(DataMenu.GetOrdinal("cIntJerarquia")).Trim
            If MenuItemPrevious Is Nothing Then MenuItemPrevious = MenuItemCurrent
            If MenuItemPreviousValue = String.Empty Then MenuItemPreviousValue = MenuItemCurrentValue
            If MenuItemUp IsNot Nothing Then MenuItemUpValue = MenuItemUp.Value

            Lx = MenuItemPreviousValue.Length
            lProx = MenuItemCurrentValue.Length

            lSup = ((lProx \ 2) - 1) * 2
            If lSup < 0 Then lSup = 0

            If Left(MenuItemCurrentValue, lSup) = Left(MenuItemPreviousValue, lSup) And InStr(1, MenuItemCurrentValue, MenuItemUpValue) > 0 Then
                If Lx < lProx Then      'AUMENTAR NIVEL
                    MenuItemTemp = MenuItemPrevious
                    MenuItemPreviousValue = String.Empty
                    FillMenu(MenuControl, DataMenu, MenuItemPreviousValue, MenuItemTemp)
                    MenuItemCurrent = AgregarMenuItem(MenuControl, MenuItemUp, DataMenu)
                Else                    'DISMINUIR O MANTENER NIVEL
                    MenuItemCurrent = AgregarMenuItem(MenuControl, MenuItemUp, DataMenu)
                End If
                If MenuItemCurrent Is Nothing Then Exit Sub
                MenuItemPreviousValue = MenuItemCurrent.Value
                MenuItemPrevious = MenuItemCurrent
            Else
                If Lx >= lProx Then Exit Sub
            End If
        Loop While DataMenu.Read()

    End Sub

    Public Function AgregarMenuItem(ByVal MenuControl As Menu, ByRef MenuItemParent As MenuItem, ByVal DataMenu As SqlDataReader) As MenuItem
        Dim MenuItemCurrent As MenuItem = Nothing
        Try
            Dim link As String = String.Empty
            If DataMenu.GetString(DataMenu.GetOrdinal("cIntNombre")).Trim = "Encuestas" Then
                link = DataMenu.GetString(DataMenu.GetOrdinal("cIntDescripcion")).Trim & "?id=" & IdentityUser.UserCode
            Else
                link = DataMenu.GetString(DataMenu.GetOrdinal("cIntDescripcion")).Trim
            End If
            MenuItemCurrent = New MenuItem(DataMenu.GetString(DataMenu.GetOrdinal("cIntNombre")).Trim, _
                                           DataMenu.GetString(DataMenu.GetOrdinal("cIntJerarquia")).Trim, _
                                           String.Empty, _
                                           link)
            If MenuItemParent Is Nothing Then
                MenuControl.Items.Add(MenuItemCurrent)
            Else
                MenuItemParent.ChildItems.Add(MenuItemCurrent)
            End If
        Catch
        End Try

        Return MenuItemCurrent
    End Function
    Public Function MostrarMensaje(ByVal Mensaje As String) As String
        Dim str As String = "<script language=""JavaScript"">alert('" & Mensaje & "');</script>"
        Return str
    End Function
End Class
